package com.wing.system.service;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.system.dao.DingTalkAttendanceBanDao;
import com.wing.system.dao.DingTalkAttendanceBanRestDao;
import com.wing.system.dao.DingTalkAttendanceBanWorkDao;
import com.wing.system.model.entity.DingTalkAttendanceBan;
import com.wing.system.model.response.DingTalkAttendanceBanRestVO;
import com.wing.system.model.response.DingTalkAttendanceBanVO;
import com.wing.system.model.response.DingTalkAttendanceBanWorkVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 *
 * @author
 */
@Service
@Slf4j
public class DingTalkAttendanceBanCiServiceImpl extends ServiceImpl<DingTalkAttendanceBanDao, DingTalkAttendanceBan> implements DingTalkAttendanceBanCiService {


    @Autowired
    DingTalkAttendanceBanWorkDao dingTalkAttendanceBanWorkDao;


    @Autowired
    DingTalkAttendanceBanRestDao dingTalkAttendanceBanRestDao;


    @Override
    public Page<DingTalkAttendanceBanVO> selDingTalkAttendanceBanInfo(Page<DingTalkAttendanceBanVO> pg, Map map) {
        List<DingTalkAttendanceBanVO> vos = baseMapper.selDingTalkAttendanceBanInfo(pg, map);
        for (DingTalkAttendanceBanVO vo : vos) {

            //获取上班时间  下班时间 组装起来
            List<DingTalkAttendanceBanWorkVO> dingTalkAttendanceBanWorkVOList = dingTalkAttendanceBanWorkDao.selAttendanceWorkList(vo.getBanId(), null);
            for (DingTalkAttendanceBanWorkVO dingTalkAttendanceBanWorkVo : dingTalkAttendanceBanWorkVOList) {
                if(dingTalkAttendanceBanWorkVo.getCheckType().equals("OnDuty")){
                    vo.setWorkBeginTime(subStringTime(dingTalkAttendanceBanWorkVo.getCheckTime()));
                }
                if(dingTalkAttendanceBanWorkVo.getCheckType().equals("OffDuty")){
                    vo.setWorkEndTime(subStringTime(dingTalkAttendanceBanWorkVo.getCheckTime()));
                }
            }

            //获取上班时间  下班时间 组装起来
            List<DingTalkAttendanceBanRestVO> dingTalkAttendanceBanRestVOList = dingTalkAttendanceBanRestDao.selAttendanceRestList(vo.getBanId(), null);
            for (DingTalkAttendanceBanRestVO dingTalkAttendanceBanRestVO : dingTalkAttendanceBanRestVOList) {
                if(dingTalkAttendanceBanRestVO.getCheckType().equals("OnDuty")){
                    vo.setRestBeginTime(subStringTime(dingTalkAttendanceBanRestVO.getCheckTime()));
                }
                if(dingTalkAttendanceBanRestVO.getCheckType().equals("OffDuty")){
                    vo.setRestEndTime(subStringTime(dingTalkAttendanceBanRestVO.getCheckTime()));
                }
            }

        }
        
        
        pg.setRecords(vos);
        return pg;
    }


    /**
     * 截取出时分秒
     * @param dateString
     * @return
     */
    public String subStringTime(String dateString){
        if(StringUtils.isNotBlank(dateString)){
            return dateString.substring(11);
        }
        return null;
    }


}
